<template>
    <div class="addadjustcourse">
        <a class="navs">申请调课</a>
        <span class="back" @click="back">返回</span>
        <el-form ref="myForm" class="addform" :model="formData" :rules="rules">
            <el-form-item label="申 请 老 师 :" prop="teacherName">
                <el-input class="inputs" v-model="teacherName" placeholder="请输入申请老师名称" disabled></el-input>
            </el-form-item>
            <el-form-item label="申 请 日 期 :" prop="repuestdate">
                <el-input class="inputs" v-model="nowTime" placeholder="请输入申请日期" disabled></el-input>
            </el-form-item>
            <el-form-item label="所 调 课 程 :" prop="changecourse">
                <el-select placeholder="请选择所调课程" v-model="formData.changecourse"
                    @change="getcourseid(formData.changecourse)">
                    <el-option v-for="item in courses" :key="item.value" :label="item.subName" :value="item.subId">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="所 调 班 级 :" prop="changeclass">
                <el-select placeholder="请选择所调班级(先选课程)" v-model="formData.changeclass"
                    @change="getclassid(formData.changeclass)">
                    <el-option v-for="item in classlist" :key="item.value" :label="item.clazzName" :value="item.clazzId">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="调 课 时 间 :" prop="newdate">
                <!-- <el-input class="inputs" v-model="formData.newdate" placeholder="请输入周几第几节"></el-input> -->
                <el-select placeholder="请选择调课时间" v-model="formData.newdate">
                    <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label=" 审 批 人 :" prop="approver" class="last">
                <el-input class="inputs" v-model="formData.approver" placeholder="请输入审批人" disabled></el-input>
            </el-form-item>
            <el-form-item label="原上课时间 :" prop="beforedate">
                <!-- <el-input class="inputs" v-model="formData.beforedate" placeholder="请输入周几第几节"></el-input> -->
                <el-select placeholder="请选择原上课时间" v-model="formData.beforedate">
                    <el-option v-for="item in onclasslist" :key="item.value" :label="item.time" :value="item.time">
                    </el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <el-row>
            <el-button type="primary" @click="addadjustcourse('myForm')" class="btn addadjustbtn">确定</el-button>
            <el-button class="btn" @click="clearinput('myForm')" type="info">取消</el-button>
        </el-row>
    </div>
</template>

<script>
export default {
    data() {
        return {
            classlist: [],
            courses: [],
            clearFlag: true,
            teacherName: "",
            nowTime: "",
            tea:[],//班级负责人信息
            formData: {
                teacherName: "",
                repuestdate: "",
                changeclass: "",
                changecourse: "",
                newdate: "",
                approver: "",
                beforedate: "",
            },
            rules: {
                // teacherName: [{ required: true, message: "申请老师不能为空", trigger: "blur" }],
                //repuestdate: [{ required: true, message: "申请日期不能为空", trigger: "blur" }],
                changeclass: [{ required: true, message: "所调班级不能为空", trigger: "change" }],
                changecourse: [{ required: true, message: "所调课程不能为空", trigger: "change" }],
                newdate: [{ required: true, message: "调课时间不能为空", trigger: "blur" }],
                approver: [{ required: true, message: "审批人不能为空", trigger: "change" }],
                beforedate: [{ required: true, message: "原上课时间不能为空", trigger: "blur" }],
            },
            approverlist: [],
            baseUrl: require("../js/baseUrl.js").baseUrl,
            userId: "",//登陆的用户的id
            courseid: "",//选中课程id
            classid: "",//选中班级id
            approverid: "",//选中审批人id
            teaId: "",//当前老师id
            pageIndex: 1,
            pageSize: 5,
            total: 0,
            onclasslist:[],//正在上的该班的课
            options:[
                {
                    value:'周1第1节课',
                    label:'周1第1节课'
                },
                {
                    value:'周1第2节课',
                    label:'周1第2节课'
                },
                {
                    value:'周1第3节课',
                    label:'周1第3节课'
                },
                {
                    value:'周1第4节课',
                    label:'周1第4节课'
                },
                {
                    value:'周1第5节课',
                    label:'周1第5节课'
                },
                {
                    value:'周2第1节课',
                    label:'周2第1节课'
                },
                {
                    value:'周2第2节课',
                    label:'周2第2节课'
                },
                {
                    value:'周2第3节课',
                    label:'周2第3节课'
                },
                {
                    value:'周2第4节课',
                    label:'周2第4节课'
                },
                {
                    value:'周2第5节课',
                    label:'周2第5节课'
                },
                {
                    value:'周3第1节课',
                    label:'周3第1节课'
                },
                {
                    value:'周3第2节课',
                    label:'周3第2节课'
                },
                {
                    value:'周3第3节课',
                    label:'周3第3节课'
                },
                {
                    value:'周3第4节课',
                    label:'周3第4节课'
                },
                {
                    value:'周3第5节课',
                    label:'周3第5节课'
                },
                {
                    value:'周4第1节课',
                    label:'周4第1节课'
                },
                {
                    value:'周4第2节课',
                    label:'周4第2节课'
                },
                {
                    value:'周4第3节课',
                    label:'周4第3节课'
                },
                {
                    value:'周4第4节课',
                    label:'周4第4节课'
                },
                {
                    value:'周4第5节课',
                    label:'周4第5节课'
                },
                {
                    value:'周5第1节课',
                    label:'周5第1节课'
                },
                {
                    value:'周5第2节课',
                    label:'周5第2节课'
                },
                {
                    value:'周5第3节课',
                    label:'周5第3节课'
                },
                {
                    value:'周5第4节课',
                    label:'周5第4节课'
                },
                {
                    value:'周5第5节课',
                    label:'周5第5节课'
                },
                {
                    value:'周6第1节课',
                    label:'周6第1节课'
                },
                {
                    value:'周6第2节课',
                    label:'周6第2节课'
                },
                {
                    value:'周6第3节课',
                    label:'周6第3节课'
                },
                {
                    value:'周6第4节课',
                    label:'周6第4节课'
                },
                {
                    value:'周6第5节课',
                    label:'周6第5节课'
                },
                {
                    value:'周日第1节课',
                    label:'周日第1节课'
                },
                {
                    value:'周日第2节课',
                    label:'周日第2节课'
                },
                {
                    value:'周日第3节课',
                    label:'周日第3节课'
                },
                {
                    value:'周日第4节课',
                    label:'周日第4节课'
                },
                {
                    value:'周日第5节课',
                    label:'周日第5节课'
                },
            ]
        }
    },
    methods: {
        back() {//返回上一级
            this.$router.push("/coursemanager/adjustcourse/adjustcourses")
        },
        //申请调课
        requestadjust() {
            this.$http.post(this.baseUrl + "SchoolSys/rea/application", {
                userId: Number(this.userId)
            }).then(res => {
                //console.log("调课申请", res);
                if (res.data.code == 200) {
                    this.teacherName = res.data.data.teas[0].teaName
                    this.courses = res.data.data.teas[0].subs
                    this.teaId = res.data.data.teas[0].teaId
                } else {
                    this.$message.error(res.data.msg)
                }
            })
        },
        //添加调课
        addadjustcourse(form) {
            this.$refs[form].validate(valid => {
                if (valid) {
                    this.$http.post(this.baseUrl + "SchoolSys/rea/addRea", {
                        teaId: this.teaId,
                        oldTime: this.formData.beforedate,
                        clazzId: this.classid,
                        newTime: this.formData.newdate,
                        subId: this.courseid,
                        userId: this.approverid,
                        createTime: this.nowTime
                    }).then(res => {
                        //console.log(res);
                        if (res.data.code == 200) {
                            this.$message({
                                message: '申请成功',
                                type: 'success'
                            });
                            this.$router.go(-1);
                        } else {
                            this.$message.error(res.data.msg)
                        }
                    })
                } else {
                    this.$message({
                        message: '您存在未填写信息！！！',
                        type: 'warning'
                    });
                }
            })
        },
        //获取选中课程id，根据课程查班级
        getcourseid(courseid) {
            this.classlist = []
            //console.log(courseid);
            this.courseid = courseid;
            //SchoolSys/rea/applicationBySubId
            this.$http.post(this.baseUrl + "SchoolSys/rea/applicationBySubId", {
                subId: this.courseid,
                teaId: this.teaId
            }).then(res => {
                //console.log("课程对应班级", res);
                if (res.data.code == 200) {
                    let classarr = res.data.data
                    //console.log(classarr);
                    classarr.forEach(v => {
                        this.classlist.push(v.clazzes[0])//申请老师的课程的班级
                    });
                   // console.log("班级列表",this.classlist);
                } else {
                    this.$message.error(res.data.msg)
                }
            })
        },
        //获取选中班级信息
        getclassid(classsection) {
            //console.log("班级id",classsection);
            this.classid=classsection
            this.classlist.map(v=>{
                if(classsection==v.clazzId){
                    this.tea=v.tea//班级负责人信息
                }
            })
            this.approverid=this.tea.teaId 
            this.formData.approver=this.tea.teaName
            this.getclasstime();
        },
        //根据老师课程班级查该老师在该班上该课程是周几第几节
        getclasstime(){
            this.$http.post(this.baseUrl + "SchoolSys/cha/getSchaById", {
                teaId:this.teaId,
                subId:this.courseid,
                clazzId:this.classid,
            }).then(res => {
                //console.log("该老师在该班上该课程是周几第几节", res);
                if (res.data.code == 200) {
                    this.onclasslist=res.data.data
                } else {
                    this.$message.error(res.data.msg)
                }
            })
        },
        //清空
        clearinput(formData) {
            this.$refs[formData].resetFields();
        },
    },
    mounted() {
        //时间
        let date = new Date();
        let dates = date.toLocaleDateString()
        this.nowTime = dates.replaceAll("/", "-")
        //获取userId
        this.userId = sessionStorage.getItem("userId");
        this.requestadjust();
    }
}
</script>

<style lang="less">
.addadjustcourse {
    position: relative;
    margin-top: 20px;
    height: 40px;
    line-height: 40px;

    .navs {
        text-decoration: none;
        margin-left: 30px;
        color: #000;
        font-size: 20px;
        font-weight: 900;
    }

    .el-row {
        width: 95%;
        margin: 200px auto 0px;

        .btn {
            width: 140px;
            height: 50px;
            font-size: 15px;
        }

        .addadjustbtn {
            background: #106ab1;
        }
    }

    .addform {
        width: 96%;
        height: 320px;
        margin: 30px auto 0px;

        .el-form-item {
            width: 34%;
            float: left;

            .el-form-item__label {
                font-size: 16px;
                font-weight: 600;
            }

            .el-select {
                width: 100%;
            }

            .el-date-editor {
                width: 100%;
            }
        }

        .el-form-item__content {
            width: 65%;
            float: left;
        }

        .last {
            .el-form-item__content {
                margin-left: 18px;
            }
        }

        .el-form-item.is-required:not(.is-no-asterisk) .el-form-item__label-wrap>.el-form-item__label:before,
        .el-form-item.is-required:not(.is-no-asterisk)>.el-form-item__label:before {
            content: "";
        }
    }

    .back {
        position: absolute;
        right: 50px;
        top: 0;
        font-size: 20px;
    }
}
</style>